#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <conio.h>
#include <windows.h>

#include "2048.h"
#include "cursor.h"

#define VERSION "Open2048 v0.2.1"
#define AUTHORS "Loveaabb"

#define DEFAULT_TNUM 2048
#define DEFAULT_TSCORE 20000
/*
 * Open2048 main program by Loveaabb<loveaabb2@163.com>
 * License: MIT
*/



void show_help(int x, int y);
void print_array(const int len, const int arr[]);
void game_over(void);
void victory(void);
void start_prompt(int* condition, Vicmode* vicmode);
void addnum(Game* game);



int main(int argc, char* argv[])
{

    srand(time(0));

    Vicmode vm = MAXNUM;
    int cond = 2048;

    start_prompt(&cond, &vm);

    Game game = Game_init(vm, cond);
    char key;
    int suc = 0;

    hSetCursor(0, 20);
    printf("%s By %s", VERSION, AUTHORS);


    hSetCursor(0, 0);
    printf("                                                             \n");
    printf("                                                             \n");
    printf("                                                             \n");
    printf("                                                             \n");
    printf("                                                             \n");
    printf("                                                             \n");
    printf("                                                             \n");
    printf("                                                             \n");
    printf("                                                             \n");
    printf("                                                             \n");
    show_help(40, 8);

    for(;;){
        suc = 0;
        Game_printBoard(&game);
        if(Game_isDead(&game))game_over();
        if(Game_isVictory(&game))victory();
        key = getch();
        switch(key){
            case 'w': suc = Game_moveUp(&game); break;
            case 's': suc = Game_moveDown(&game); break;
            case 'a': suc = Game_moveLeft(&game); break;
            case 'd': suc = Game_moveRight(&game); break;
            case 'r': game = Game_init(game.vicmode, game.condition); suc = 0; break;
            default:suc = 0;
        }
        if(suc)addnum(&game);
    }
    return 0;
}


void show_help(int x, int y){
    hSetCursor(x + 2, y);
    printf("W");
    hSetCursor(x, y + 2);
    printf("A   D");
    hSetCursor(x + 2, y + 4);
    printf("S");
}

void print_array(const int len, const int arr[]){
    printf("{");
    for(int i = 0; i < len; i++){
        printf("%d", arr[i]);
        if(i<len - 1)printf(", ");
    }
    printf("}");
}

void addnum(Game* game){
    if(rng(1, 10) > 1){
        Game_addNum(game, 2);
    }
    else{
        Game_addNum(game, 4);
    }
}

void game_over(void){
    hSetCursor(0, 18);
    printf("GAME OVER! qwq\n");
    hSetCursor(0, 24);
    getchar();
    exit(0);
}

void start_prompt(int* condition, Vicmode* vicmode){
    char vm;
    char ccond[11] = {'\0'};

    printf("Please enter game mode (maxnum / score / none) [maxnum]: ");
    vm = getchar();
    if(vm == 'n' || vm == 'N'){
        *vicmode = NONE;
        printf("  this will be an endless game");
    }
    else if(vm == 's' || vm == 'S'){
        *vicmode = SCORE;
        printf("  selected mode: Score");
    }
    else{
        *vicmode = MAXNUM;
        printf("  selected mode: Max Number");
    }
    fflush(stdin);


    printf("\n");
    if(*vicmode == MAXNUM){
        printf("Enter the target number [%d]: ", DEFAULT_TNUM);
        gets(ccond);
        if(ccond[0] < '0' || ccond[0] > '9'){
            *condition = DEFAULT_TNUM;
            fflush(stdin);
            return;
        }
    }
    else if(*vicmode == SCORE){
        printf("Enter the target score [%d]: ", DEFAULT_TSCORE);
        gets(ccond);
        if(ccond[0] < '0' || ccond[0] > '9'){
            *condition = DEFAULT_TSCORE;
            fflush(stdin);
            return;
        }
    }
    else{
        condition = 0;
        return;
    }
    *condition = atoi(ccond);
    if(*condition <= 4) *condition = 8;
    fflush(stdin);
}

void victory(void){
    hSetCursor(0, 18);
    printf("YOU WIN!!!\nCONGRATULATIONS ;3");
    hSetCursor(0, 24);
    getchar();
    exit(0);
}
